package com.woniuxy.service.impl;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.woniuxy.bean.RoleBean;
import com.woniuxy.dao.RoleDao;
import com.woniuxy.dao.UserDao;
import com.woniuxy.pojo.Role;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.Contants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserDao userDao;
    @Override
    public User getUserByUserName(String userName) {
        return userDao.getUserByUserName(userName);
    }

    @Override
    public User getLogin(String name, String password) {
        Subject subject=SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(name,password);
        try {
            subject.login(token);
            Session session=subject.getSession();
            User sessionUser=this.getUserByUserName(name);
            session.setAttribute(Contants.SESSION_USER,sessionUser);
            return sessionUser;
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public List<User> getUserListByPage(Integer currentPage, Integer pageSize,String userName) {
        Integer start = (currentPage-1)*pageSize;
        return userDao.getUserListByPage(start,pageSize,userName);
    }

    @Override
    public Integer getTotalSize(String userName) {
        System.out.println(userName+"###########################");
        return userDao.getTotalSize(userName);
    }

    @Override
    public Integer getPages(Integer totalSize, Integer pageSize) {
        return totalSize%pageSize==0?totalSize/pageSize:totalSize/pageSize+1;
    }
    @Override
    public List<RoleBean> getRoleBeansByUserId(Integer userId) {
        List<RoleBean> all = new ArrayList<RoleBean>();
        List<Role> roleAll = roleDao.getAllRoles();
        Integer[] roleIds = roleDao.getRoleByUserId(userId);
        if (roleAll!=null&&!roleAll.isEmpty()){
            for (Role role : roleAll) {
                RoleBean roleBean = new RoleBean();
                roleBean.setRole(role);
                for (int i=0;i < roleIds.length;i++){
                    if(roleIds[i].equals(role.getId())){
                        roleBean.setChecked("checked='checked'");
                    }
                }
                all.add(roleBean);
            }
        }
        return all;
    }

    @Override
    public Boolean assighUserRole(Integer userId,List<Integer> roleIds) {
        Boolean flag = false;
        //删除角色
        try {
            this.roleDao.delete(userId);
            //添加角色
            for (Integer roleId:roleIds) {
                Map<String,Integer> map = new HashMap<String,Integer>(2);
                map.put("userId",userId);
                map.put("roleId",roleId);
                roleDao.save(map);
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    @Override
    public void add(User user) {
        if(user.getPassword()==null){
            user.setPassword("1234");
        }
        this.userDao.add(user);
    }

    @Override
    public void delete(Integer id) {
        this.userDao.delete(id);
    }

    @Override
    public User findOne(Integer id) {
        return userDao.findOne(id);
    }

    @Override
    public void upd(User user) {
        userDao.upd(user);
    }
}
